package com.finalist.controller.admin;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.finalist.Message;
import com.finalist.entity.EditorTemplate;
import com.finalist.service.EditorTemplateService;

@Controller("editorTemplateController")
@RequestMapping("/admin/editor_template")
public class EditorTemplateController extends BaseController{

	@Resource(name = "editorTemplateServiceImpl" )
	private EditorTemplateService editorTemplateService;
	
	/**
	 * 列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(ModelMap model) {
		model.addAttribute("editorTemplates", editorTemplateService.findAll());
		return "/admin/editor_template/list";
	}
	
	
	/**
	 * 添加
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap model) {
		
		return "/admin/editor_template/add";
	}
	
	/**
	 * 保存
	 * @param editorTemplate
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public String save(EditorTemplate editorTemplate,RedirectAttributes redirectAttributes){
		Pattern p = Pattern.compile("\t|\r|\n");
        Matcher m = p.matcher(editorTemplate.getContent());
        editorTemplate.setContent(m.replaceAll(""));
		editorTemplateService.save(editorTemplate);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody
	Message delete(Long id) {
		editorTemplateService.delete(id);
		return SUCCESS_MESSAGE;
	}
	
	/**
	 * 编辑
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value ="/edit",method = RequestMethod.GET)
	public String edit(Long id,ModelMap model){
		model.addAttribute("editorTemplate",editorTemplateService.find(id));
		return "/admin/editor_template/edit";
	}
	
	/**
	 * 更新
	 * @param editorTemplate
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public String update(EditorTemplate editorTemplate,RedirectAttributes redirectAttributes){
		Pattern p = Pattern.compile("\t|\r|\n");
        Matcher m = p.matcher(editorTemplate.getContent());
        editorTemplate.setContent(m.replaceAll(""));
		editorTemplateService.update(editorTemplate);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}
	
	@RequestMapping(value = "/check_name", method = RequestMethod.GET)
	public @ResponseBody
	boolean checkName(String name) {
		if (StringUtils.isEmpty(name)) {
			return false;
		}
		if (editorTemplateService.nameExists(name)) {
			return false;
		} else {
			return true;
		}
	}
	
}
